package com.springcloud.demo.test.tree;


import org.apache.commons.collections.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author zhouyang
 * @Description
 * @date 2018/09/30
 */
public class TreeUtils {

    public static <T extends TreeTest> List<T> listToTree(List<T> list){
        Map<String, List<T>> collect = list.stream().collect(Collectors.groupingBy(T::getPid));
        list.stream().forEach(x -> {
            if(!CollectionUtils.isEmpty(collect.get(x.getId()))){
                x.setChildren(collect.get(x.getId()));
            }
        });
        return list;
    }


}
